<template>
  <div class="sky-inr">
    <Card dis-hover :bordered="false">
      <Button
        v-hasC="{
          name: 'rbac-data-permission-create',
          flag: 'v1/rbac/data-permission/create',
        }"
        type="primary"
        class="ivu-mb-8"
        @click="handleAdd('add')"
        >新增</Button
      >
      <Table highlight-row :columns="columns" :data="shareData">
        <template slot="status" slot-scope="{ row }">
          <Badge v-if="row.status === '1'" status="success" text="正常" />
          <Badge v-else status="error" text="停用" />
        </template>
        <template slot="operation" slot-scope="{ row }">
          <a
            v-hasC="{
              name: 'rbac-data-permission-update',
              flag: 'v1/rbac/data-permission/update',
            }"
            @click="handleAdd('edit', row)"
            >编辑</a
          >
          <Divider
            v-hasCC="[
              {
                name: 'rbac-data-permission-update',
                flag: 'v1/rbac/data-permission/update',
              },
              {
                name: 'rbac-data-permission-delete',
                flag: 'v1/rbac/data-permission/delete',
              },
            ]"
            v-if="row.status === '0'"
            type="vertical"
          />
          <a
            v-hasC="{
              name: 'rbac-data-permission-delete',
              flag: 'v1/rbac/data-permission/delete',
            }"
            v-if="row.status === '0'"
            @click="handleDelete(row)"
            >删除</a
          >
        </template>
      </Table>
    </Card>
    <Modal
      :mask-closable="false"
      v-model="addModal"
      :title="share ? '新增' : '编辑'"
    >
      <Form
        ref="form"
        :labelWidth="85"
        :v-model="formItem"
        :model="formItem"
        :rules="ruleValidate"
      >
        <FormItem label="数据来源：" :label-width="100" prop="data_source_id">
          <Select v-model="formItem.data_source_id" v-width="100">
            <Option
              v-for="item in data_source"
              :value="item.value"
              :key="item.value"
              >{{ item.title }}</Option
            >
          </Select>
          <Select
            :label-width="100"
            v-if="formItem.data_source_id"
            v-model="formItem.source"
            class="ivu-ml-8"
            v-width="200"
          >
            <Option
              v-for="item in is_select_data_source"
              :value="item.id"
              :key="item.id"
              >{{ item.label }}</Option
            >
          </Select>
        </FormItem>
        <FormItem label="共享数据：" :label-width="100" prop="share_menu_id">
          <Select
            v-model="formItem.share_menu_id"
            placeholder="请选择数据模块"
            v-width="310"
          >
            <Option
              v-for="item in share_menu"
              :value="item.value"
              :key="item.value"
              >{{ item.title }}</Option
            >
          </Select>
        </FormItem>
        <FormItem label="共享到：" :label-width="100" prop="shared_source_id">
          <Select v-model="formItem.shared_source_id" v-width="100">
            <Option
              v-for="item in shared_source"
              :value="item.value"
              :key="item.value"
              >{{ item.title }}</Option
            >
          </Select>
          <Select
            v-if="formItem.shared_source_id"
            v-model="formItem.shared_source"
            class="ivu-ml-8"
            v-width="200"
          >
            <Option
              v-for="item in is_select_shared_source"
              :value="item.id"
              :key="item.id"
              >{{ item.label }}</Option
            >
          </Select>
        </FormItem>
        <FormItem label="状态：" v-if="!share">
          <i-switch v-model="formItem.status" size="large">
            <span slot="open">On</span>
            <span slot="close">Off</span>
          </i-switch>
        </FormItem>
      </Form>
      <div slot="footer">
        <Button type="text" @click="cancel">取消</Button>
        <Button type="primary" :loading="remarksloading" @click="handleSubmit">确定</Button>
      </div>
    </Modal>
  </div>
</template>
<script>
import {
  dataPermissionList,
  addDataPermission,
  editDataPermission,
  deleteDataPermission,
  getDataPermissionInfo,
} from "@/api/TApi/index.js";
export default {
  name: "dataPower",
  data() {
    const validate1 = (rule, value, callback) => {
      if (!value || !this.formItem.source) {
        callback(new Error("请完善数据"));
      } else {
        callback();
      }
    };
    const validate2 = (rule, value, callback) => {
      if (!value || !this.formItem.shared_source) {
        callback(new Error("请完善数据"));
      } else {
        callback();
      }
    };
    return {
      // 新增或者编辑params
      formItem: {
        //  数据来源第一个选项
        data_source_id: "",
        //  数据来源第二个选项
        source: "",
        // 共享数据
        share_menu_id: "",
        // 共享到第一个数据
        shared_source_id: "",
        // 共享到第二个数据
        shared_source: "",
        // 状态
        status: "",
      },
      isEditId: "",
      addModal: false,
      remarksloading: false,
      selectModal: false,
      shartLabel: "",
      dataSource: "部门",
      sharePower: "只读",
      share: true,
      typeList: [
        {
          value: "部门",
          label: "部门",
        },
        {
          value: "人员",
          label: "人员",
        },
      ],
      userList: [
        {
          value: "咨询部",
          label: "咨询部",
        },
        {
          value: "客服部",
          label: "客服部",
        },
      ],
      typeList1: [
        {
          value: "部门",
          label: "部门",
        },
        {
          value: "人员",
          label: "人员",
        },
      ],
      userList1: [
        {
          value: "咨询部",
          label: "咨询部",
        },
        {
          value: "客服部",
          label: "客服部",
        },
      ],
      sourceList: [
        {
          value: "咨询部",
          label: "咨询部",
        },
        {
          value: "回访部",
          label: "回访部",
        },
      ],
      personList: [
        {
          value: "高玲",
          label: "高玲",
        },
        {
          value: "王艳",
          label: "王艳",
        },
      ],
      powerList: [
        {
          value: "咨询员",
          label: "咨询员",
        },
        {
          value: "回访员",
          label: "回访员",
        },
      ],
      dataModule: [
        {
          value: "患者列表",
          label: "患者列表",
        },
        {
          value: "线索列表",
          label: "线索列表",
        },
      ],
      columns: [
        {
          title: "数据来源",
          key: "data_source_name",
          width: 125,
        },
        {
          title: "共享到",
          key: "shared_source_name",
          width: 200,
        },
        {
          title: "状态",
          slot: "status",
          width: 150,
        },
        {
          title: "操作",
          slot: "operation",
          minWidth: 125,
        },
      ],
      // 列表数据
      shareData: [],
      // select下拉信息
      // 数据来源
      data_source: [],
      // 共享数据
      share_menu: [],
      // 共享到
      shared_source: [],
      // 表单验证
      ruleValidate: {
        data_source_id: {
          trigger: "change",
          required: true,
          validator: validate1,
        },
        share_menu_id: {
          required: true,
          message: "请完善数据",
        },
        shared_source_id: {
          trigger: "change",
          required: true,
          validator: validate2,
        },
      },
    };
  },
  created() {
    this._dataPermissionList();
    this._getDataPermissionInfo();
  },
  computed: {
    is_select_data_source() {
      return this.data_source.find(
        (ee) => ee.value == this.formItem.data_source_id
      )
        ? this.data_source.find(
            (ee) => ee.value === this.formItem.data_source_id
          ).list
        : [];
    },
    is_select_shared_source() {
      return this.shared_source.find(
        (ee) => ee.value == this.formItem.shared_source_id
      )
        ? this.data_source.find(
            (ee) => ee.value === this.formItem.shared_source_id
          ).list
        : [];
    },
  },
  methods: {
    cancel() {
      this.addModal = false;
    },
    //   编辑或新增确定按钮
    handleSubmit() {
      let params = {
        data_source_type: this.formItem.data_source_id,
        data_source_id: this.formItem.source,
        share_menu_id: this.formItem.share_menu_id,
        shared_source_type: this.formItem.shared_source_id,
        shared_source_id: this.formItem.shared_source,
        status: 1,
      };
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.remarksloading = true
          setTimeout(()=>{
            this.remarksloading = false
          },1000)
          let params = {
            data_source_type: this.formItem.data_source_id,
            data_source_id: this.formItem.source,
            share_menu_id: this.formItem.share_menu_id,
            shared_source_type: this.formItem.shared_source_id,
            shared_source_id: this.formItem.shared_source,
            status: 1,
          };
          switch (this.share) {
            case true:
              {
                addDataPermission(params).then((res) => {
                  this.$Message.success("新增成功");
                  this._dataPermissionList();
                  this.addModal = false;
                });
              }
              break;
            case false:
              {
                params.id = this.isEditId;
                params.status = this.formItem.status === true ? 1 : 0;
                editDataPermission(params).then((res) => {
                  this.$Message.success("编辑成功");
                  this._dataPermissionList();
                  this.addModal = false;
                });
              }
              break;
          }
        }
      });
    },
    //   初始化列表
    _dataPermissionList() {
      dataPermissionList().then((res) => {
        this.shareData = Object.freeze(res.list);
      });
    },
    //   初始化下拉信息
    _getDataPermissionInfo() {
      getDataPermissionInfo().then((res) => {
        this.data_source = res.data_source;
        this.share_menu = res.share_menu;
        this.shared_source = res.shared_source;
      });
    },
    // 新增编辑打开窗口
    handleAdd(type, data) {
      this.addModal = true;
      this.$refs.form.resetFields();
      this.formItem.source = "";
      this.formItem.shared_source = "";
      switch (type) {
        case "add":
          {
            this.share = true;
          }
          break;
        case "edit":
          {
            this.share = false;
            this.isEditId = data.id;
            this.$nextTick(() => {
              this.formItem.data_source_id = Number(data.data_source_type);
              this.formItem.source = data.data_source_id;
              this.formItem.share_menu_id = Number(data.share_menu_id);
              this.formItem.shared_source_id = Number(data.shared_source_type);
              this.formItem.shared_source = data.shared_source_id;
              this.formItem.status = data.status == "1" ? true : false;
            });
          }
          break;
      }
    },

    handleDelete(data) {
      this.$Modal.confirm({
        title: "删除确认",
        content: "<p>是否确认删除此条共享数据</p>",
        okText: "确认",
        onOk: () => {
          deleteDataPermission({
            id: data.id,
          }).then((res) => {
            this.$Message.success("删除成功");
            this._dataPermissionList();
          });
        },
      });
    },
    handleEven() {
      this.shartLabel.color = "default";
    },
  },
};
</script>
<style>
.sky-inr .ivu-tabs-bar {
  border: none;
  margin-bottom: 0px;
}
</style>
